﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace CNVP.Cache
{
    public class FileCaches : FileCacheBase
    {
        /// <summary>
        /// 自动处理页面缓存
        /// 当返回true表示要中断页面的所有内容
        /// false不要做处理
        /// 会重Response.Write
        /// </summary>
        /// <param name="httpcontext">请传入当前页的HttpContext</param>
        /// <returns></returns>
        public bool CacheProgress(HttpContext httpcontext)
        {
            return this.CacheProgress(httpcontext, false, true);
        }
        /// <summary>
        /// 自动处理页面缓存
        /// 当返回true表示要中断页面的所有内容
        /// false不要做处理
        /// 会重Response.Write
        /// </summary>
        /// <param name="httpcontent">请传入当前页的HttpContext</param>
        /// <param name="IsReCache">是否强制重写Cahce</param>
        /// <returns></returns>
        public bool CacheProgress(HttpContext httpcontext, bool IsReCache)
        {
            return CacheProgress(httpcontext, IsReCache, true);
        }
        /// <summary>
        /// 自动处理页面缓存
        /// 当返回true表示要中断页面的所有内容
        /// false不要做处理
        /// </summary>
        /// <param name="httpcontent">请传入当前页的HttpContext</param>
        /// <param name="IsReCache">是否强制重写Cahce</param>
        /// <param name="IsRewritePage">是否重写页面内容 Response.Write 当为False时，只是重新生成静态文件，页面内容不会变动</param>
        /// <returns></returns>
        public bool CacheProgress(HttpContext httpcontext, bool IsReCache, bool IsRewritePage)
        {
            if (httpcontext.Request.QueryString["serverget"] == "1")
            {
                return false;
            }

            string strContent = "";
            string strUrl = httpcontext.Request.Url.ToString();
            string strDomain = httpcontext.Request.Url.Host;

            if (this.ExistCache(strUrl, strDomain) == false || IsReCache == true)
            {
                //取当前页面的HTML
                strContent = this.GetUrlResponse(strUrl);
                strContent += "\r\n<!--JShop File Cache Has Been Opened -->";
                //存缓存
                this.SetCache(strContent, strUrl, strDomain);
            }
            else
            {
                //取缓存
                strContent = this.GetCache(strUrl, strDomain);
            }
            //输出内容,当是重写Cache
            if (IsRewritePage)
            {
                httpcontext.Response.Write(strContent);
                httpcontext.Response.End();
            }
            return true;
        }
        /// <summary>
        /// 保存Cache
        /// </summary>
        /// <param name="p_Content"></param>
        /// <param name="p_Url">页面地址</param>
        /// <param name="p_Domain">域名</param>
        public void SetCache(string p_Content, string p_Url, string p_Domain)
        {
            string fileNameNoExt = GetFileNameNoExt(p_Url, p_Domain);
            //写在这里			
            this.SaveFile(fileNameNoExt, p_Content);
        }
        /// <summary>
        /// 检查Cache文件是否存在或过期
        /// </summary>
        /// <param name="p_Url">页面地址</param>
        /// <param name="p_Domain">域名</param>
        /// <returns></returns>
        public bool ExistCache(string p_Url, string p_Domain)
        {
            string fileNameNoExt = GetFileNameNoExt(p_Url, p_Domain);

            return this.ExistFile(fileNameNoExt);
        }
        /// <summary>
        /// 取Cache,请在使用之间先用 ExistCache 确定Cache存在
        /// </summary>
        /// <param name="p_Url">页面地址</param>
        /// <param name="p_Domain">域名</param>
        /// <returns></returns>
        public string GetCache(string p_Url, string p_Domain)
        {
            string strContent = "";
            string fileNameNoExt = GetFileNameNoExt(p_Url, p_Domain);
            this.ReadFile(fileNameNoExt, ref strContent);
            return strContent;
        }
    }
}